<template>
  <div class="login">
    <div class="box">
      <img src="../assets/1.gif" alt />

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
           
<script>
import { login_api } from "@/api/index.js";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },

      // 表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 登录
    submitForm(formName) {
      // 获取form表单 对表单进行校验
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 如果valid为true 校验满足规则可发送请求
          // this.axios.post("/login", this.ruleForm).then(res => {
          //   console.log(res);
          //   if (res.data.meta.status === 200) {
          //     //成功
          //     this.$message({
          //       message: res.data.meta.msg,
          //       type: "success"
          //     });
          //     // alert(res.data.meta.msg);
          //     this.$router.push("/home");
          //   } else {
          //     this.$message.error(res.data.meta.msg);
          //     // alert(res.data.meta.msg);
          //   }
          // });

          const res = await login_api(this.ruleForm);
          if (res.data.meta.status === 200) {
            //成功
            this.$message({
              message: res.data.meta.msg,
              type: "success"
            });
            // 本地缓存 token
            // console.log(res, 5);
            window.sessionStorage.setItem("token", res.data.data.token);
             this.$router.push("/home");
          } else {
            this.$message.error(res.data.meta.msg);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 重置
    resetForm(formName) {
      // this.$refs
      // console.log(this.$refs);
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped lang="less">
/* scoped 作用域 */
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .el-form {
      padding: 100px 20px;
    }
  }

  .btns {
    text-align: right;
  }

  //   图片
  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    top: -5%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 20px #dddddd;
  }
}
</style>